'''
@auther:zhongyingjie
@date:开发时间:2022/11/21 17:06
@file:request_datas.PY
@desc:
'''
import time
import pytest
import requests
from phpwind.common.request_define import *
from phpwind.common.config import *

# 请求头
req_header = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'}

# 1、首页
@pytest.fixture()
def homepage():
    shouye = {
        'url': 'http://{}/phpwind/index.php?m=bbs'.format(config.Host),
        'except': '立即注册'  # 断言期望
    }
    return shouye

# 获取token
@pytest.fixture()
def csrf_token(init_session_obj):
    url = "http://{}/phpwind/".format(config.Host)
    csrf_token_zz = 'name="csrf_token" value="(.+?)"/>'
    res = init_session_obj.create_request(req_type='GET', url=url)
    csrf_token = init_session_obj.patter_find(csrf_token_zz, res)
    return csrf_token
# 2、登录
login_datas = [{'username': 'c8513294n', 'password': 'cn19961011', 'except': '成功提示'},
               {'username': 'c8513294', 'password': 'cn19961011.', 'except': '不存在'},
               {'username': 'c8513294n', 'password': 'cn1996101', 'except': '密码错误'},
               {'username': '', 'password': 'cn19961011.', 'except': '错误提示'},
               {'username': 'c8513294n', 'password': '', 'except': '错误提示'},
               {'username': 'sanwy', 'password': '123456', 'except': '错误提示'}]
@pytest.fixture(params=login_datas)
def test_login_01(init_session_obj, request,csrf_token):
    username = request.param['username']
    password = request.param['password']
    exce = request.param['except']
    login = {
        'url': 'http://{}/phpwind/index.php?m=u&c=login&a=dologin'.format(config.Host),
        'data': {'username': username,
                 'password': password,
                 'csrf_token': csrf_token},
        'except': exce  # 断言期望
            }
    return login

# 3、获取登录状态
status_datas1 = [{'username': 'sanwy', 'password': '12138wy.', 'except': '成功提示'}]
@pytest.fixture(params=status_datas1)
def test_login_statu(init_session_obj, request,csrf_token):
    username = request.param['username']
    password = request.param['password']
    exce = request.param['except']
    url='http://{}/phpwind/index.php?m=u&c=login&a=dologin'.format(config.Host)
    data={'username': username,
                 'password': password,
                 'csrf_token': csrf_token,
                 'csrf_token': csrf_token}

    login_res = init_session_obj.create_request(req_type='POST',
                                                url=url,
                                                data=data,
                                                headers=req_header)
    statu_zz = '_statu=(.+?)"'
    statu = init_session_obj.patter_find(statu_zz, login_res)
    return statu
status_datas1 = [{ 'except': '本站新帖'}]
@pytest.fixture(params=status_datas1)
def get_login_statu(init_session_obj,request,test_login_statu):
    exec = request.param['except']
    get_status = {
        'url': 'http://{}/phpwind/index.php?m=u&c=login&a=welcome'.format(config.Host),
        'param': {'_statu':test_login_statu},
        'except': exec  # 断言期望
    }
    return get_status

# 4、发帖
timeSTR=time.strftime("%Y%m%d%H%M%S")
title_str='发贴标题'+timeSTR
content_str='发帖内容'+timeSTR
ft_data = [{'title1':title_str,'content1':content_str,'except':'success'},
           {'title1':title_str,'content1':'','except':'fail'},
           {'title1':'','content1':content_str,'except':'fail'},
           {'title1':'..','content1':content_str,'except':'fail'},
           {'title1':title_str,'content1':'..','except':'fail'},
           {'title1':'','content1':'','except':'fail'}]
@pytest.fixture(params=ft_data)
def send_content(csrf_token,get_login_statu,request):
    title=request.param.get('title1')
    content = request.param.get("content1")
    exce = request.param.get("except")

    ft_data = {'url':'http://{}/phpwind/index.php?c=post&a=doadd&_json=1&fid=10'.format(config.Host),
               'data':{'atc_title':title,
               'atc_content':content,
               'pid':'','tid':'','special':'default','reply_notice':'1',
               'csrf_token':csrf_token},
               'except':exec}
    return ft_data

# 回帖
huitie = {
    # 'url': 'http://{}/phpwind/index.php?m=u&c=login&a=dologin'.format(config.Host),
    # 'except': '账号'  # 断言期望
}
# 退出
logout = {
    # 'url': 'http://{}/phpwind/index.php?m=u&c=login&a=dologin'.format(config.Host),
    # 'except': '账号'  # 断言期望
}
